//手机号码正则表达式
var phoneReg=/^1[345789][0-9]{9}$/;
//验证码正则表达式
var codeReg = /^[0-9]{6}$/;
var pswReg0 = /^.{6,16}$/;
var pswReg1 = /[0-9]+/;
var pswReg2 = /[a-zA-Z]+/;
var pswReg3 = /[=~!@#\$%\^&\*\(\)_\+\{\[\}\]:"\|',<?.\/\\]+/;
var timer;
var t = 60;
//表示是否同意注册协议
var isAgree = false;
var isPhoneRight = false;
var isYZMRight = false;
var isPsw1Right = false;
var isPsw2Right = false;
var phoneerror = document.getElementById("phoneerror");
//功能1：向用户提示手机号码格式是否正确
phone.onblur=function(){
    var phoneValue = phone.value;
    if(!phoneReg.test(phoneValue)){
        phoneerror.innerHTML="手机号码格式有误！";
        isPhoneRight = false;

    }else{
        phoneerror.innerHTML = "手机号码格式正确！";
        isPhoneRight = true;
    }

    //
    checkIsHavePhone();

    checkAll();
};
function checkIsHavePhone(){
    $.get("http://test.hfjava.com/shop/userphone?phone="+phone.value,{},function(data){
        if(data=="false"){
            phoneerror.innerHTML = "此号码已经注册！";
            isPhoneRight = false;
        }
    });
}

//功能2：根据手机号码格式的正确性，决定是否启用验证码按钮
phone.onkeyup = function(){
    var phoneValue = phone.value;
    if(!phoneReg.test(phoneValue)){
        yanzhengmaBtn.setAttribute("disabled","disabled");
        yanzhengmaBtn.style.color = "";
        isPhoneRight = false;
    }else{
        yanzhengmaBtn.removeAttribute("disabled");
        yanzhengmaBtn.style.color = "red";
        isPhoneRight = true;
    }
    checkAll();
};
//功能3：发送验证码功能
//点击后要求倒计时60秒，在此期间该按钮禁用
yanzhengmaBtn.onclick = function(){
    yanzhengmaBtn.setAttribute("disabled","disabled");
    yanzhengmaBtn.style.color = "";
    yanzhengmaBtn.value = "重新发送60";
    timer = setInterval(daojishi,1000);
};
function daojishi(){
    t--;
    if(t==0){
        clearInterval(timer);
        yanzhengmaBtn.value = "发送验证码";
        yangzhengmaBtn.style.color = "red";
        t = 60;
        return;
    }
    yanzhengmaBtn.value = "重新发送"+t;

}
//功能4：验证验证码正确性
yzm.onkeyup = function(){
    var yzmValue = yzm.value;
    if(yzmValue.length==0){
        yzmerror.innneHTML = "请您输入验证码！";
        isYZMRight = false;
        checkAll();
        return;
    }
    console.log(codeReg.test(yzmValue));
    if(!codeReg.test(yzmValue)){
        yzmerror.innerHTML = "验证码格式有误！";
        isYZMRight = false;
        checkAll();
        return;
    }
    $.ajax({
        url:"http://test.hfjava.com/shop/yzm",
        dataType:"jsonp",
        success:function(data){

            if(data.code!=yzmValue){
                yzmerror.innerHTML = "验证码错误！";
                isYZMRight = false;
            }else{
                yzmerror.innerHTML = "验证码正确！";
                isYZMRight = true;

            }

            checkAll();
        }
    })
};
//功能5：密码1验证格式有效性
psw1.onkeyup = function(){
    var psw1Value = psw1.value;
    var rightCount = 0;
    //长度 字母 数字 符号
    var lengthValid = pswReg0.test(psw1Value);
    var numberValid = pswReg1.test(psw1Value);
    var letterValid = pswReg2.test(psw1Value);
    var symbolValid = pswReg3.test(psw1Value);

    if(numberValid){
        rightCount++;
    }
    if(letterValid){
        rightCount++;
    }
    if(symbolValid){
        rightCount++;
    }
    if(!lengthValid||rightCount<2){
        pswerror1.innerHTML = "密码格式错误";
        isPsw1Right = false;
    }else{
        pswerror1.innerHTML = "密码格式正确";
        isPsw1Right = true;
    }
    checkPsw2();
    checkAll();
};
 //功能6：密码2验证码与密码1是否一致
psw2.onkeyup = function(){
    checkPsw2();
};

function checkPsw2(){
    var psw1Value = psw1.value;
    var psw2Value = psw2.value;
    if(psw1Value!==psw2Value){
        pswerror2.innerHTML = "二次输入密码不一致";
        isPsw2Right = false;
    }else{
        pswerror2.innerHTML = "二次输入密码一致";
        isPsw2Right = true;
    }
    checkAll();
}

//功能7：获得用户是否同意注册协议
agreeCK.onclick = function(){
    isAgree = this.checked;
    checkAll();
};

//功能8：检测是否可以注册
function checkAll(){
    var isRight = isAgree&&isPhoneRight&&isYZMRight&&isPsw1Right&&isPsw2Right;

    if(isRight){
        submitBtn.removeAttribute("disabled");
        submitBtn.style.color = "red";
    }else{
        submitBtn.setAttribute("disabled","disabled");
        submitBtn.style.color = "";
    }
}

//功能9：实现注册功能
submitBtn.onclick = function(){
    $.post("http://test.hfjava.com/shop/adduser",
        $("form").serialize(),
        function(data){
            if(data=="Saved"){
                alert("注册成功！");
            }
        }
    )
};